﻿using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Taoyl.CodeTraceModels;
using xbd.DataConvertLib;

namespace Taoyl.CodeTraceDAL
{
    public class HourProductionService
    {
        /// <summary>
        /// 日产量统计
        /// </summary>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <returns></returns>
        public OperateResult<List<ProductionData>, DayProduction> GetDayStatics(int month,int day)
        {
            List<ProductionData> productionDatas = new List<ProductionData>();
            //要查询的月份的第一秒
            DateTime dayStart = Convert.ToDateTime(DateTime.Now.AddMonths(month - DateTime.Now.Month).AddDays(day-DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00"));
            int totalOK = 0;
            int totalNG = 0;

            for (int i = 0; i < 24; i++)
            {
                int okCount = SqlSugarHelper.SqlSugarClient.Queryable<HourProduction>().Where(c=>c.HourTime==dayStart.AddHours(i)).Sum(c => c.OKCount);
                int ngCount = SqlSugarHelper.SqlSugarClient.Queryable<HourProduction>().Where(c => c.HourTime == dayStart.AddHours(i)).Sum(c => c.NGCount);

                productionDatas.Add(new ProductionData($"{i}-{i + 1}", okCount, ngCount));

                totalOK += okCount;
                totalNG += ngCount;


            }
            return OperateResult.CreateSuccessResult(productionDatas, new DayProduction(totalOK + totalNG, totalOK, totalNG));
        }



        /// <summary>
        /// 月统计信息
        /// </summary>
        /// <param name="month"></param>
        /// <returns></returns>
        public OperateResult<List<ProductionData>, MonthProduction> GetMonthStatics(int month)
        {
            List<ProductionData> productionDatas = new List<ProductionData>();

            //要查询的月份的第一秒
            DateTime monthStart = Convert.ToDateTime(DateTime.Now.AddMonths(month - DateTime.Now.Month).ToString("yyyy-MM-dd 00:00:00"));

            int totalOK = 0;
            int totalNG = 0;

            for (int i = 0; i < 31; i++)
            {
                DateTime start = monthStart.AddDays(i);
                DateTime end = monthStart.AddDays(i + 1);

                int okCount=SqlSugarHelper.SqlSugarClient.Queryable<HourProduction>().Where(c=>SqlFunc.Between(c.HourTime, start, end)).Sum(c=>c.OKCount);
                int ngCount = SqlSugarHelper.SqlSugarClient.Queryable<HourProduction>().Where(c => SqlFunc.Between(c.HourTime, start, end)).Sum(c => c.NGCount);
           
                productionDatas.Add(new ProductionData($"{i+1}日",okCount, ngCount));

                totalOK += okCount;
                totalNG += ngCount;
            
            }
            return OperateResult.CreateSuccessResult(productionDatas,new MonthProduction(totalOK+totalNG,totalOK,totalNG));
        }


        /// <summary>
        /// 更新小时数据表
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public int UpdateHourProduction(bool result)
        {
            DateTime current = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:00:00"));

            HourProduction production = SqlSugarHelper.SqlSugarClient.
                                                    Queryable<HourProduction>().
                                                    Where(c => c.HourTime == current).
                                                    First();

            if (production != null)
            {
                //更新
                if (result)
                {
                    production.OKCount++;
                }
                else
                {
                    production.NGCount++;
                }
                return SqlSugarHelper.SqlSugarClient.Updateable(production).WhereColumns(c => c.HourTime).ExecuteCommand();
            }
            else
            {
                HourProduction production1 = new HourProduction()
                {
                    HourTime = current,
                    OKCount = result ? 1 : 0,
                    NGCount = result ? 0 : 1
                };
                return SqlSugarHelper.SqlSugarClient.Insertable(production1).ExecuteCommand();



            }
        }

    }
}
